Skip to main content

Sync local drive folders with rclone

· 2 min read

Sync local folders with rclone

A command line script to synchronize local folders

Prerequisites and Config

  • Download rclone
  • Copy content of download archive into a folder that is in your path environment variable - e.g. C:\Windows\System32

Synchronize your folder

The rclone sync command allows you to sync from source to target without changing any source files. rclone has many more powerful options - see its Docs page

In the below PowerShell script we define an array of source and target folders and a flag if that source folder should be synchronized:

Sync local folders with rclone
# Set the path to the local folder to synchronize
# rclone sync --interactive /home/source /tmp/destination

$dataStructures = @(
[PSCustomObject]@{ # BastianDev
sourceFolder = "C:\SourceFolder01"
targetFolder = "D:\TargetFolder01"
syncFolder = $true
},
[PSCustomObject]@{ # BastianDev
sourceFolder = "C:\SourceFolder02"
targetFolder = "D:\TargetFolder02"
syncFolder = $false
},
[PSCustomObject]@{ # BastianDev
sourceFolder = "C:\SourceFolder03"
targetFolder = "D:\TargetFolder03"
syncFolder = $true
}
# add as many new objects here as needed
)

# Iterate over the array of data structures
foreach ($dataStructure in $dataStructures) {
$source = $($dataStructure.sourceFolder)
$target = $($dataStructure.targetFolder)
$sync = $($dataStructure.syncFolder)
$rcloneSyncCmd = 'rclone sync "' + $source + '" "' + $target + '" --stats-log-level NOTICE'
if ($sync) {
Write-Host "Sync '$source' to '$target'" -ForegroundColor Green
Invoke-Expression $rcloneSyncCmd
} else {
Write-Host "Skipping sync of '$source' due to sync flag set to false" -ForegroundColor DarkYellow
}
}

# Wait for keypress to finish
Write-Host ""
Write-Host "****************************************" -ForegroundColor Green
Write-Host "Press key to close script" -ForegroundColor Green
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode

Save the script to a .ps1 file, adjust your folders and execute!